package com.yuncool.jx3robot.common.util;


import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImageUtils {

    private static float jPEGcompression = 0.75f;// 图片清晰比率

    public static void getFunctionImage() throws IOException {
        //原素材
        File file = new File("/Users/yuncool/Downloads/q1.jpg");
        // 加载原图图片
        BufferedImage imageLocal = ImageIO.read(file);
        // 以原图片为模板
        Graphics2D g = imageLocal.createGraphics();
        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setComposite(ac);
        g.setBackground(Color.WHITE);

        g.setColor(Color.BLACK);
        // 计算文字长度，计算居中的x点坐标
        // 设置文本样式
        g.setFont(new Font("AaMianHuatang", Font.BOLD, 100));
        g.drawString("笨猪机器人", 275, 150);
        // 设置文本样式
        g.setFont(new Font("AaMianHuatang", Font.BOLD, 50));
        g.drawString("日常", 100, 250);
        g.drawString("开服", 250, 250);
        g.drawString("更新公告", 400, 250);
        g.drawString("沙盘", 550 + 100, 250);
        g.drawString("金价", 550 + 100 + 150, 250);
        //换行
        g.drawString("骚图", 100, 350);
        g.drawString("夸我", 250, 350);
        g.drawString("骂他(@某人)", 400, 350);
        g.drawString("服务器 ", 550 + 200, 350);
        //换行
        g.drawString("开启(关闭)暖场", 100, 450);
        g.drawString("成就 成就名称", 250 + 250, 450);
        //换行
        g.drawString("宏 jy(惊羽)", 100, 550);
        g.drawString("小药 jy(惊羽)", 250 + 150, 550);
        g.drawString("物价 五红", 250 + 480, 550);
        //换行
        g.drawString("pve jy(惊羽)", 100, 650);
        g.drawString("pvp jy(惊羽)", 250 + 200, 650);
        //换行
        g.drawString("前置 三山四海", 100, 750);
        g.drawString("资历排行 [门派(可选)] ", 100 + 350, 750);
        //换行
        g.drawString("奇遇 [ID(可选)] [奇遇名称(可选)]", 100, 850);
        g.drawString("花价 [鲜花(可选)] [线路(可选)]", 100, 950);
        g.drawString("宠物攻略 宠物or宠物奇遇名称", 100, 1050);
        g.drawString("资历(全服)排行 [门派(可选)]", 100, 1150);
        g.setFont(new Font("AaMianHuatang", Font.BOLD, 30));
        g.drawString("电五双梦出大铁灯萝!需要密聊!", 50, 1280);
        g.drawString("本机器人由双梦木桩团贡献～", imageLocal.getWidth() - 430, 1280);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        saveAsJPEG(imageLocal, out);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/Users/yuncool/Downloads/q3.jpg"));
        byte[] bytes = out.toByteArray();
        bos.write(bytes);
        bos.close();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        getFunctionImage();
    }

    public static void saveAsJPEG(BufferedImage imageToSave, ByteArrayOutputStream fos) throws IOException {
        ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();
        ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
        imageWriter.setOutput(ios);
        if (jPEGcompression >= 0 && jPEGcompression <= 1f) {
            // new Compression
            JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
            jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
            jpegParams.setCompressionQuality(jPEGcompression);

        }
        // new Write and clean up
        ImageIO.setUseCache(false);
        imageWriter.write(new IIOImage(imageToSave, null, null));
        ios.close();
        imageWriter.dispose();
    }
}
